package org.noote.libs.assistant.analyse;


// http://www.synapse-fr.com/manuels/ATM_0.htm

public class AnalyseGrammaticale {

	String m_sPhraseOrigine;

	Liste_Elements _elements = new Liste_Elements();
	Liste_Phrases _phrases = new Liste_Phrases();

	public boolean analyse(Ponctuation ponctuation, Dictionnaire dictionnaire, Grammaire grammaire, String sPhrase) {
		m_sPhraseOrigine = sPhrase;
		
		_elements.clear();
		_phrases.clear();

		return doAnalyse(ponctuation, dictionnaire, grammaire, transmute(ponctuation, m_sPhraseOrigine));
	}
	
	public Liste_Elements getElements()
	{
		return _elements;
	}
	
	public Liste_Phrases getPhrases()
	{
		return _phrases;
	}

	protected boolean doAnalyse(Ponctuation ponctuation, Dictionnaire dictionnaire, Grammaire grammaire, String sPhrase) {
		String listeMot[] = sPhrase.split("\\ ");
		
		// extraction des elements
		for(String element : listeMot)
		{
			_elements.add(Element.analyse(ponctuation, dictionnaire, element));
		}
		
		// extraction en phrase
		grammaire.extrairePhrases(_elements, _phrases);
		// analyse des phrases
		grammaire.analyse(_phrases);
		
		// analyse semantique

		return true;
	}

	protected String transmute(Ponctuation ponctuation, String sString) {
		String sTransmuted = sString;

		// remove doublon, tabulation
		boolean noMoreTransmutation = true;
		while (noMoreTransmutation) {
			noMoreTransmutation = false;
			if (sTransmuted.contains("/t"))
			{
				sTransmuted = sTransmuted.replace("/t", " ");
				noMoreTransmutation = true;
			}
			if (sTransmuted.contains("/r"))
			{
				sTransmuted = sTransmuted.replace("/r", " ");
				noMoreTransmutation = true;
			}
			if (sTransmuted.contains("/n"))
			{
				sTransmuted = sTransmuted.replace("/n", " ");
				noMoreTransmutation = true;
			}
			if (sTransmuted.contains("  "))
			{
				sTransmuted = sTransmuted.replace("  ", " ");
				noMoreTransmutation = true;
			}
			for(Element_Ponctuation ponc : ponctuation._liste)
			{
				String sP = ponc._element+ponc._element;
				if (sTransmuted.contains(sP))
				{
					sTransmuted = sTransmuted.replace(sP, ponc._element);
					noMoreTransmutation = true;
				}
			}
		}
		
		for(Element_Ponctuation ponc : ponctuation._liste)
		{
			sTransmuted = sTransmuted.replace(ponc._element, " "+ponc._element+" ");
		}

		return sTransmuted;
	}
}
